package cn.itcast.service;

import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.MemberDao;
import cn.itcast.dao.OrderDao;
import cn.itcast.dao.OrderSettingDao;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*
 *Author:TH
 *Dete:2019/10/2716:53
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao menberDao;
    @Autowired
    private OrderDao orderDao;

    //新增预约
    public Result order(Map map) throws Exception{
        //得到预约时间
        String orderDate = (String) map.get("orderDate");
        //判断预约时间是否能够预约
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting==null){
            //该时间不可以预约,预约失败
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断预约人数是否满员
        if (orderSetting.getReservations()>=orderSetting.getNumber()){
            //预约人数已满
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //获取手机号
        String telephone = (String) map.get("telephone");
        //判断用户是否为会员
        Member member = new Member();
        member = menberDao.findByTelephone(telephone);
        if (member!=null){
            //是会员,进行预约判断
            Integer memberId = member.getId();
            Date date = DateUtils.parseString2Date(orderDate);
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
            Order order = new Order(memberId, date, setmealId);
            //判断是否重复预约
            List<Order> list = orderDao.findByCondition(order);
            if (list!=null){
                //重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }

        }else{
            //非会员,注册会员
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            menberDao.add(member);
        }
        //预约成功，更新当日的已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
        orderDao.add(order);

        orderSetting.setReservations(orderSetting.getReservations() + 1);//设置已预约人数+1
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    //根据预约ID查询预约相关信息（体检人姓名、预约日期、套餐名称、预约类型）
    public Map findById(Integer id) throws Exception{
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
